package dart

class ClassificationController {
	def index = { redirect(action: listClasses) }

//--List Classes---------------------------------------------------------------
	def listClasses = {
		return params
	}

//--Edit Class-----------------------------------------------------------------

	def editClass = {
		Classification classification = Classification.get(params.id)
		if (!classification) {
			flash.message = "A classification was not found with the id ${params.id}."
			redirect(action: listClasses)
			return
		}

		return [classification: classification]
	}

	def update = {
		Classification classification = Classification.get(params.id)

		classification.properties['name'] = params
		if (!classification.hasErrors() && classification.save(flush: true)) {
			redirect(action: listClasses)
		} else {
			flash.message = "There was a problem creating this class"
			flash.error = classification.errors.fieldError
			render(view: 'editClass', model: [classification: classification])
		}
	}

	def flipEnabled = {
		Classification classification = Classification.get(params.id)

		classification.enabled = !classification.enabled
		if (classification.save(flush: true)) {
			redirect(action: listClasses)
		} else {
			flash.message = "There was a problem changing this class"
			flash.error = classification.errors.fieldError
			render(view: 'editClass', model: [classification: classification])
		}
	}

//--Create Class---------------------------------------------------------------
	def createClass = {
		Classification classification = new Classification(params)
		return [classification: classification]
	}

	def save = {
		Classification classification = new Classification(params)
		if (!classification.hasErrors() && classification.save(flush: true)) {
			redirect(action: listClasses)
		} else {
			flash.message = "There was a problem creating this class"
			flash.error = classification.errors.fieldError
			render(view: 'createClass', model: [classification: classification])
		}
	}
}